<template>
    <div class="app-container">
      <!-- 科室列表设置 -->
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
            <el-input v-model="searchObj.keyword" placeholder="姓名电话"></el-input>
        </el-form-item>
        <el-form-item label="创建时间">
            <el-date-picker
                v-model="createTime"
                type="daterange"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
            </el-date-picker>
        </el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="findByPage()">查询</el-button>
        <el-button type="default" @click="resetData()">清空</el-button>
      </el-form>
      <!-- 工具条 -->
      <div>
        <el-button type="danger" size="mini" @click="batchDelete()">批量删除</el-button>
      </div>
      <el-table
        :data="list"
        stripe
        style="width: 100%"
        @selection-change="handleSelection">
        <el-table-column type="selection" width="50" />
        <el-table-column type="index" label="序号" width="50" />
        <el-table-column prop="nickName" label="昵称" width="120"  />
        <el-table-column prop="phone" label="手机号" width="120" />
        <el-table-column prop="name" label="用户姓名" width="120" />
        <el-table-column prop="certificatesType" label="证件类型" width="150" />
        <el-table-column prop="certificatesNo" label="证件编号" width="150" />
        <el-table-column prop="certificatesUrl" label="证件路径" width="120" />
        <el-table-column prop="authStatus" label="认证状态" width="120" >
            <template slot-scope="scope">
                <span v-show="scope.row.authStatus === 0">未认证</span>
                <span v-show="scope.row.authStatus === 1">认证中</span>
                <span v-show="scope.row.authStatus === 2">认证成功</span>
                <span v-show="scope.row.authStatus === -1">认证失败</span>
            </template>
        </el-table-column>
        <el-table-column label="状态" width="160">
            <template slot-scope="scope">
            {{ scope.row.status === 0 ? "锁定" : "正常" }}
            </template>
        </el-table-column>
        <el-table-column label="操作" width="300" align="center" fixed="right">
          <template slot-scope="scope">
            <!-- <el-button type="danger" size="mini"
            icon="el-icon-delete" @click="deleteById(scope.row.id)">删除</el-button> -->
            <router-link :to="'/user/show/'+scope.row.id">
                <el-button type="primary" size="mini">查看</el-button>
            </router-link>
            <el-button v-if="scope.row.status==1" type="warning" size="mini"
                icon="el-icon-lock" @click="lockById(scope.row.id, 0)">锁定</el-button>
            <el-button v-if="scope.row.status==0" type="success" size="mini"
                icon="el-icon-unlock" @click="lockById(scope.row.id, 1)">取消锁定</el-button>
            <router-link :to="'/user/edit/'+scope.row.id" >
              <el-button type="primary" size="mini" class="margin10"
              icon="el-icon-edit">编辑</el-button>
            </router-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        :current-page="current"
        :page-size="limit"
        :total="total"
        style="padding: 30px 0; text-align: center;"
        layout="total, prev, pager, next, jumper"
        @current-change="findByPage"/>
    </div>
  </template>
  
  <style>
    .margin10 {
      margin-left: 10px;
      margin-right: 10px;
    }
  </style>
  
  <script>
  // 引入js文件
  import user from '@/api/user'
  
  export default {
    // 定义初始值和变量
    data() {
      return {
        createTime: [],//创建时间
        current: 1, // 当前页
        limit: 5, // 每页数量
        searchObj: {}, // 条件查询对象
        list: [], // 每页数据集合
        total: 0, // 总记录数
        selectedIds: [] // 选中的数据
      }
    },
    // 在页面渲染之前执行
    created() {
      // 一般调用methods定义的方法，得到数据
      this.findByPage()
    },
    // 定义方法。进行请求接口调用
    methods: {
      // 科室列表
      findByPage(page = 1) {
        this.current = page
        this.searchObj.createTimeBegin = this.createTime[0]
        this.searchObj.createTimeEnd = this.createTime[1]
        user.findByPage(this.current, this.limit, this.searchObj)
          .then((response) => {
            // 请求成功后调用
            // console.log(response)
            this.list = response.data.records
            this.total = response.data.total
            console.log(this.list)
          })
          .catch((error) => {
            // 请求失败后调用
            console.log(error)
          })
      },
      //根据id删除数据
      deleteById(id) {
        this.$confirm('确认删除数据？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {//点击确定按钮
          //调用接口删除数据
          user.deleteById(id)
          .then((response) => {
            //提示信息
            this.$message({
              type: 'success',
              message: '删除成功！'
            })
            //刷新列表数据
            this.findByPage(1)
          })
          .catch((error) => {
            // 请求失败后调用
            console.log(error)
          })
        }).catch((error) => {
          
        })
      },
      //锁定或取消锁定
          lockById(id, status) {
              user.lockById(id, status).then((response) => {
                      //刷新列表数据
                      this.findByPage(1)
                  })
          },
          //查看详情
          showDetail(id) {
            userApi.findById(id).then((response) => {
                this.user = response.data
                // 赋值
                this.authStatus = '' + this.user.authStatus
                this.status = '' + this.user.status
            })
          },
      //获取复选框选中的值
      handleSelection(selection) {
        this.selectedIds = []
        for(var i = 0; i < selection.length; i++) {
          this.selectedIds.push(selection[i].id)
        }
      },
      //批量删除
      batchDelete() {
        if (this.selectedIds.length == 0) {
          this.$message({
            message: '请先选择要删除的数据',
            type: 'warning'
          })
          return
        }
        this.$confirm('确认删除数据？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {//点击确定按钮
          //调用接口删除数据
          user.batchDelete(this.selectedIds)
          .then((response) => {
            //提示信息
            this.$message({
              type: 'success',
              message: '删除成功！'
            })
            //刷新列表数据
            this.findByPage(1)
          })
          .catch((error) => {
            // 请求失败后调用
            console.log(error)
          })
        }).catch((error) => {
  
        })
      },
      //清空数据
      resetData() {
            this.searchObj = {}
            this.createTime = []
        }
    }
  }
  </script>
  